<!-- 腾讯电子健康卡 -->
<template>
  <web-view :src="pageUrl" />
</template>

<script setup>
import { onLoad } from "@dcloudio/uni-app";
import { ref } from "vue";
import healthCard from "../static/ElectronicHealthCard";
import healthCardUtil from "@/utils/healthCard/index";
import { HEALTH_CARD_TYPE } from "@/utils/healthCard/constant";

const pageUrl = ref("");

onLoad((options) => {
  init(options);
});

async function init(options) {
  console.log("【options】", options);
  const { type, wechatCode } = options;
  switch (type) {
    // 建档
    case HEALTH_CARD_TYPE.CREATE:
      handleCreate(wechatCode);
      break;
    // 升级
    case HEALTH_CARD_TYPE.UPDATE:
      handleUpdate(wechatCode);
      break;
    // 绑定验证
    case HEALTH_CARD_TYPE.BIND_VERIFY:
      handleBind();
      break;
    // 查询验证
    case HEALTH_CARD_TYPE.SEARCH_VERIFY:
      handleSearchVerify(wechatCode);
      break;
    default:
      break;
  }
}
// 新增
const handleCreate = async (wechatCode) => {
  try {
    healthCardUtil.showLoad();
    await healthCard.beforeCreateCard({ wechatCode });
    pageUrl.value = healthCard.createURL;
    healthCardUtil.hideLoad();
    console.log("【pageUrl】", pageUrl.value);
  } catch (err) {
    healthCardUtil.hideLoad(1500);
  }
};
// 升级
const handleUpdate = async (wechatCode) => {
  try {
    healthCardUtil.showLoad();
    await healthCard.beforeUpdateCard({
      wechatCode,
      patientId: healthCardUtil.patientId,
    });
    pageUrl.value = healthCard.updateURL;
    healthCardUtil.hideLoad();
    console.log("【pageUrl】", pageUrl.value);
  } catch (err) {
    healthCardUtil.hideLoad(1500);
  }
};
// 绑定
const handleBind = () => {
  healthCardUtil.showLoad();
  pageUrl.value = healthCard.bindVerifyURL;
  healthCardUtil.hideLoad();
};
// 校验
const handleSearchVerify = async (wechatCode) => {
  try {
    healthCardUtil.showLoad();
    const { name, idType, idNumber } = healthCardUtil.transferInfoByPatientInfo(
      healthCardUtil.patientInfo
    );

    const { data } = await healthCard.beforeGenerateOrderIdVerify({
      wechatCode,
      name,
      cardType: idType,
      idCard: idNumber,
      scene: healthCardUtil.scene,
      useCardType: healthCardUtil.getCardType(
        healthCardUtil.patientInfo.healthCardId
      ),
    });
    const { verifyUrl, verifyOrderId } = data;
    // 记录校验数据
    healthCardUtil.recordVerifyOrderId(verifyOrderId);
    pageUrl.value = verifyUrl;
    healthCardUtil.hideLoad();
    console.log("【pageUrl】", pageUrl.value);
  } catch (err) {
    healthCardUtil.hideLoad(1500);
  }
};
</script>
